<template>
  <div class="personExcel">
    <div class="world">①&nbsp;代理人excel上传</div>
    <el-form class="formPer">
      <el-form-item label="选择代理人：">
        <el-select v-model="value" @change="changeSelect" placeholder="请选择">
          <el-option
            class="select"
            v-for="item in personArr"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="uploadBlock" @click="openWindowUpload">
      <i v-show="uploadShow" class="el-icon-upload"></i>
      <div class="uploaded" v-show="!uploadShow">
        <img class="uploadedImg" src="@/assets/xlsx1.png" />
        <span>{{ excelName }}</span>
      </div>
      <input type="file" accept=".xlsx" id="file-uploader" @change="onChange" multiple />
    </div>
  </div>
</template>

<script>
const { ipcRenderer } = window.require("electron");
export default {
  components: {},
  /* props: {
    resetValue: {
      type: Boolean,
      default: true,
    },
  }, */
/*   watch: {
    resetValue(newVal) {
      this.uploadShow = newVal;
    },
    uploadShow(newVal) {
      this.$emit('resetMiddle')
    }
  }, */
  data() {
    return {
      personArr: ["周昊", "周涛", "纪工", "韩工", "王工", "邵工", "许工"],
      value: "",
      excelName: "",
      uploadShow: true,
    };
  },
  methods: {
    // 呼起上传
    onChange(e) {
      this.ipcSendFiles(e.target.files);
      document.getElementById("file-uploader").value = "";
    },

    // 监听
    changeSelect() {
      this.uploadShow = true;
      this.$emit("givePersonData", []);
    },
    // 通信main进程
    ipcSendFiles(files) {
      let sendFiles = [];
      Array.from(files).forEach((item) => {
        let obj = {
          name: item.name,
          path: item.path,
        };
        sendFiles.push(obj);
      });
      console.log(sendFiles);
      this.$emit("givePersonData", sendFiles);
      this.checkExcel(sendFiles);
    },

    // 呼起上传
    openWindowUpload() {
      if (!this.value) {
        this.$message({
          type: "error",
          message: "请先选择代理人",
          offset: 60,
        });
      } else {
        document.getElementById("file-uploader").click();
      }
    },

    // 校验格式
    checkExcel(files) {
      const path = files[0].path;
      if (path.indexOf(".xlsx") === -1) {
        this.$message({
          type: "error",
          message: "请选择xlsx格式的excel文件",
          offset: 60,
        });
      } else {
        this.excelName = files[0].path;
        files[0].person = this.value;
        this.uploadShow = false;
        ipcRenderer.send("uploadChooseExcel", files);
      }
    },
  },
};
</script>

<style lang="stylus">
.el-input__inner {
  background: #21252b !important;
  color: #fff !important;
}

.el-form-item__label {
  color: #fff !important;
}

.el-select-dropdown__list {
  background: #21252b !important;
}

.el-popper[x-placement^=bottom] .popper__arrow::after {
  border-bottom-color: #21252b !important;
}

.el-select-dropdown {
  border: none !important;
}

.el-select-dropdown__item {
  color: #fff !important;
}

.el-select-dropdown__item.hover, .el-select-dropdown__item:hover {
  background-color: #282c34 !important;
}

.formPer {
  height: 100px;
  padding-left: 20px;
}

.uploaded {
  margin-top: -10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;

  span {
    color: #fff;
    display: block;
    font-size: 15px;
    display: block;
    width: 94%;
    line-height: 1.3;
    margin: 20px auto 0 auto;
    word-wrap: break-word;
    word-break: break-all;
    white-space: pre-wrap !important;
  }
}

.uploadedImg {
  width: 81px;
}
</style>
